<template>
	<view class="profile-stats" @click="myDaka">
		<view>
			<h4>{{ totalDays }} Day</h4>
			<p>已打卡天数</p>
		</view>
		<view>
			<h4>{{money}} 元</h4>
			<p>已赚金额</p>
		</view>
	</view>
</template>

<script>
	import {
		globalVariable
	} from '../../configMy.js';
	export default {
		name: "day",
		props: {
			teacher: {
				type: Object, // 根据teacher的数据类型进行设置
				required: true,
			}
		},
		data() {
			return {
				teacherChecks: [],
				totalDays: 0,
				moneys: 80,
				money2: 0
			};
		},
		mounted() {
			let that = this;

			uni.request({
				url: globalVariable + "/getAllTeacherCheck?teacherId=" + this.teacher.teacherId,
				success(res) {
					let info = res.data.data;
					that.teacherChecks = info;
					that.calculateDays();
				}
			})

			//设置金额
			if (this.teacher.teacherId == 2 || this.teacher.teacherId == 4 || this.teacher.teacherId == 5) {
				this.moneys -= 10;
			}

			uni.request({
				url: globalVariable + "/getAppointmentByTeacherId?teacherId=" + this.teacher.teacherId,
				success(res) {
					let info = res.data;
					if (info.status == 1) {
						info.data.forEach(stu => {
							if (stu.isFinish != 0) {
								that.money2 += Number(stu.money)
							}

						})
					} else {
						uni.showToast({
							title: info.message,
							icon: 'none'
						});
					}
				},
				fail(err) {
					uni.showToast({
						title: '获取预约信息失败',
						icon: 'none'
					});
				}
			});
		},
		computed: {
			money() {
				let result= this.totalDays * this.moneys + this.money2;
				//这是打卡的工资
				let formattedResult = parseFloat(result.toFixed(2));

				// 返回结果  
				return formattedResult;

			}
		},
		methods: {
			myDaka() {
				let that = this;
				let teacherChecks = JSON.stringify(this.teacherChecks);
				uni.navigateTo({
					url: "/pages/myDaka/myDaka?teacherChecks=" + encodeURIComponent(teacherChecks) + "&moneys=" +
						encodeURIComponent(this.moneys) + "&teacherId=" + encodeURIComponent(that.teacher
							.teacherId),
				})
			},
			calculateDays() {
				let total = 0;
				this.teacherChecks.forEach(check => {
					let checkInTime = new Date(check.checkInTime);
					let checkOutTime = new Date(check.checkOutTime);

					if ((checkInTime.getHours() >= 7 && checkInTime.getHours() <= 9) && (checkOutTime.getHours() >=
							11 && checkOutTime.getHours() <= 14)) {
						total += 0.5;
					} else if ((checkInTime.getHours() >= 14 && checkInTime.getHours() <= 16) && (checkOutTime
							.getHours() >= 18 && checkOutTime.getHours() <= 21)) {
						total += 0.5;
					} else if ((checkInTime.getHours() >= 7 && checkInTime.getHours() <= 9) && (checkOutTime
							.getHours() >= 18 && checkOutTime.getHours() <= 21)) {
						total += 1;
					}
				});
				this.totalDays = total;
			}
		}
	}
</script>

<style>
	.profile-stats {
		display: flex;
		justify-content: space-around;
		margin-bottom: 20px;
	}

	.profile-stats view {
		flex: 1;
		padding: 10px;
		border-right: 1px solid #ddd;
	}

	.profile-stats view:last-child {
		border-right: none;
	}
</style>